//
//  UIScrollView+MY_MJRefresh.m
//  Zhuangpin
//
//  Created by Robin on 2016/10/9.
//  Copyright © 2016年 linbei. All rights reserved.
//

#import "UIScrollView+MY_MJRefresh.h"
#import "MJRefreshStateHeader.h"

@implementation UIScrollView (MY_MJRefresh)

#pragma mark - header methods
- (void)addRefreshBlock:(void (^)())block
{
    if ([self.mj_header isKindOfClass:[MJRefreshGifHeader class]]){
        MJRefreshGifHeader* header = (id)self.mj_header;
        header.refreshingBlock = block;
    }
    else{
        self.mj_header = [MJRefreshGifHeader headerWithRefreshingBlock:block];
        [self headerSetting];
    }
}

- (void)addRefreshTarget:(id)target action:(SEL)action
{
    if ([self.mj_header isKindOfClass:[MJRefreshGifHeader class]]){
        MJRefreshGifHeader* header = (id)self.mj_header;
        [header setRefreshingTarget:target refreshingAction:action];
    }
    else{
        self.mj_header = [MJRefreshGifHeader headerWithRefreshingTarget:target refreshingAction:action];
        self.mj_header.automaticallyChangeAlpha = NO;
        [self headerSetting];
    }
}

- (void)beginRefreshing
{
    if (self.mj_header.state == MJRefreshStateRefreshing){
        return;
    }
    [self.mj_header beginRefreshing];
}

- (void)endRefreshing
{
    [self.mj_header endRefreshing];
}

- (BOOL)isRefreshing
{
    return self.mj_header.isRefreshing;
}

#pragma mark - Footer methods
- (void)addLoadMoreBlock:(void (^)())block
{
    if (block == nil) {
        self.mj_footer = nil;
        return;
    }
    if ([self.mj_footer isKindOfClass:[MJRefreshBackGifFooter class]]){
        MJRefreshBackGifFooter* footer = (id)self.mj_footer;
        footer.refreshingBlock = block;
    }
    else{
        self.mj_footer = [MJRefreshBackGifFooter footerWithRefreshingBlock:block];
        self.mj_footer.automaticallyChangeAlpha = NO;
        self.mj_footer.automaticallyHidden = NO;
        [self footerSetting];
    }
//    if ([self.mj_footer isKindOfClass:[MJRefreshBackNormalFooter class]]){
//        MJRefreshBackNormalFooter* footer = (id)self.mj_footer;
//        footer.refreshingBlock = block;
//    }
//    else{
//        self.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:block];
//        self.mj_footer.automaticallyChangeAlpha = NO;
//        self.mj_footer.automaticallyHidden = NO;
//        [self footerSetting];
//    }
}

- (void)addLoadMoreTarget:(id)target action:(SEL)action
{
    if ([self.mj_footer isKindOfClass:[MJRefreshBackGifFooter class]]){
        MJRefreshBackGifFooter* footer = (id)self.mj_footer;
        [footer setRefreshingTarget:target refreshingAction:action];
    }
    else{
        self.mj_footer = [MJRefreshBackGifFooter footerWithRefreshingTarget:target refreshingAction:action];
        [self footerSetting];
    }
//    if ([self.mj_footer isKindOfClass:[MJRefreshBackNormalFooter class]]){
//        MJRefreshBackNormalFooter* footer = (id)self.mj_footer;
//        [footer setRefreshingTarget:target refreshingAction:action];
//    }
//    else{
//        self.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:target refreshingAction:action];
//        [self footerSetting];
//    }
}

-(void)beginLoadMore
{
    if (self.mj_footer.state == MJRefreshStateRefreshing){
        return;
    }
    [self.mj_footer beginRefreshing];
}

- (BOOL)isLoadMoreing
{
    return self.mj_footer.isRefreshing;
}

- (void)endLoadMore
{
    [self.mj_footer endRefreshing];
}

- (void)endRefreshAndLoadMore
{
    [self endRefreshing];
    [self endLoadMore];
}

#pragma mark - 初始化数据
- (void)headerSetting
{
    MJRefreshGifHeader* header = (id)self.mj_header;
//    header.stateLabel.hidden = YES;
    header.stateLabel.font = MYFont(Font24);
    header.stateLabel.textColor = HEXCOLOR(HSColorGray);
    header.lastUpdatedTimeLabel.hidden = YES;
//        header.lastUpdatedTimeLabel.font = TTFont(Font24);
//        header.lastUpdatedTimeLabel.textColor = COLOR_KEY(ML_Color_Black);
//    UIImage *idle = [UIImage imageNamed:@"cat.gif"];
//    [header setImages:idle.images duration:idle.duration * 2 forState:MJRefreshStateIdle];
//    [header setImages:idle.images duration:idle.duration * 0.7 forState:MJRefreshStatePulling];
//    [header setImages:idle.images duration:idle.duration * 0.7 forState:MJRefreshStateRefreshing];
    // 设置普通状态的动画图片
    NSMutableArray *idleImages = [NSMutableArray array];
    for (NSUInteger i = 1; i<=60; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"dropdown_anim__000%zd", i]];
        [idleImages addObject:image];
    }
    [header setImages:idleImages forState:MJRefreshStateIdle];
    
    // 设置即将刷新状态的动画图片（一松开就会刷新的状态）
    NSMutableArray *refreshingImages = [NSMutableArray array];
    for (NSUInteger i = 1; i<=3; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"dropdown_loading_0%zd", i]];
        [refreshingImages addObject:image];
    }
    [header setImages:refreshingImages forState:MJRefreshStatePulling];
    
    // 设置正在刷新状态的动画图片
    [header setImages:refreshingImages forState:MJRefreshStateRefreshing];
}

- (void)footerSetting
{
    MJRefreshBackGifFooter* footer = (id)self.mj_footer;
    footer.stateLabel.font = MYFont(Font24);
    footer.stateLabel.textColor = HEXCOLOR(HSColorGray);
    [footer setTitle:@"上拉可以加载更多" forState:MJRefreshStateIdle];
    [footer setTitle:@"正在加载更多,稍后哦!" forState:MJRefreshStateRefreshing];
    [footer setTitle:@"已全部加载咯!" forState:MJRefreshStateNoMoreData];
    footer.stateLabel.font = [UIFont systemFontOfSize:12];
}

@end

